package vn.sms.lazylove.database;

import java.util.ArrayList;
import java.util.List;

import vn.sms.lazylove.object.OneSMS;
import vn.sms.lazylove.object.SchedulerSMSItem;
import vn.sms.lazylove.object.SendSms;
import vn.sms.lazylove.object.Sms;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseManager extends AbstractDatabaseManager {
	public static final String LOG_TAG = DatabaseManager.class.getSimpleName();

	/* ----------------------------------------------------------------------- */
	/* Static fields */
	/* ----------------------------------------------------------------------- */
	public static final String DATABASE_NAME = "lazy_love.db";
	public static final int DATABASE_VERSION = 1;

	// Fields
	public static final String FIELD_SMSPATENT_KEY = "id";
	public static final String FIELD_SMSPATENT_MESSAGE = "message";

	public static final String FIELD_SCHEDULERSMS_ID = "id";
	public static final String FIELD_SCHEDULERSMS_NAME = "name";
	public static final String FIELD_SCHEDULERSMS_PHONE = "phone";
	public static final String FIELD_SCHEDULERSMS_MESSAGE = "message";
	public static final String FIELD_SCHEDULERSMS_WEEK = "week";
	public static final String FIELD_SCHEDULERSMS_TIME = "time";
	public static final String FIELD_SCHEDULERSMS_STATUS = "status";

	public static final String FIELD_WEEKONESMS_KEY = "id";
	public static final String FIELD_WEEKONESMS_RANDOM = "random";
	public static final String FIELD_WEEKONESMS_FLAG = "flag";

	public static final String FIELD_SAVESENDSMS_ID = "id";
	public static final String FIELD_SAVESENDSMS_MESSAGE = "message";
	public static final String FIELD_SAVESENDSMS_PHONE = "phone";
	public static final String FIELD_SAVESENDSMS_TIME = "time";

	// Tables
	public static final String TABLE_SMSPATENT = "SMSPatent";
	public static final String TABLE_SCHEDULERSMS = "SchedulerSMS";
	public static final String TABLE_WEEKONESMS = "WeekOneSMS";
	public static final String TABLE_SAVESENDSMS = "SaveSendSMS";

	private static DatabaseManager writeAdapter;

	protected DatabaseManager(Context ctx, boolean allowWrite) {
		super(ctx, allowWrite);
		// TODO Auto-generated constructor stub
	}

	public static DatabaseManager getWritableInstance(Context ctx) {
		// TODO: get writable database
		if (writeAdapter == null)
			writeAdapter = new DatabaseManager(ctx, true);
		return writeAdapter;
	}

	public static DatabaseManager getReadableInstance(Context ctx) {
		// TODO: Get readable database
		return new DatabaseManager(ctx, false);
	}

	@Override
	public void onCreateDatabase(SQLiteDatabase db) {
		// TODO: Create database and index tables
		super.onCreateDatabase(db);
	}

	/* ----------------------------------------------------------------------- */
	/* Abstracts methods */
	/* ----------------------------------------------------------------------- */
	@Override
	public String getDatabaseName() {
		return DATABASE_NAME;
	}

	@Override
	public int getDatabaseVersion() {
		return DATABASE_VERSION;
	}

	@Override
	protected String[] getSQLCreateTables() {
		String sqlSMSPatent = getSQLCreateTable(TABLE_SMSPATENT, new SQLPair(
				FIELD_SMSPATENT_KEY, "INTEGER PRIMARY KEY autoincrement"),
				new SQLPair(FIELD_SMSPATENT_MESSAGE, "TEXT NOT NULL"));
		String sqlSchedulerSMS = getSQLCreateTable(TABLE_SCHEDULERSMS,
				new SQLPair(FIELD_SCHEDULERSMS_ID,
						"INTEGER PRIMARY KEY autoincrement"), new SQLPair(
						FIELD_SCHEDULERSMS_NAME, "TEXT NOT NULL"), new SQLPair(
						FIELD_SCHEDULERSMS_PHONE, "TEXT NOT NULL"),
				new SQLPair(FIELD_SCHEDULERSMS_MESSAGE, "TEXT NOT NULL"),
				new SQLPair(FIELD_SCHEDULERSMS_WEEK, "TEXT NOT NULL"),
				new SQLPair(FIELD_SCHEDULERSMS_TIME, "TEXT NOT NULL"),
				new SQLPair(FIELD_SCHEDULERSMS_STATUS, "INTEGER NOT NULL"));
		String sqlWeekOneSMS = getSQLCreateTable(TABLE_WEEKONESMS, new SQLPair(
				FIELD_WEEKONESMS_KEY, "INTEGER PRIMARY KEY autoincrement"),
				new SQLPair(FIELD_WEEKONESMS_RANDOM, "INTEGER NOT NULL"),
				new SQLPair(FIELD_WEEKONESMS_FLAG, "INTEGER NOT NULL"));
		String sqlSaveSendSMS = getSQLCreateTable(TABLE_SAVESENDSMS,
				new SQLPair(FIELD_SAVESENDSMS_ID,
						"INTEGER PRIMARY KEY autoincrement"), new SQLPair(
						FIELD_SAVESENDSMS_MESSAGE, "TEXT NOT NULL"),
				new SQLPair(FIELD_SAVESENDSMS_PHONE, "TEXT NOT NULL"),
				new SQLPair(FIELD_SAVESENDSMS_TIME, "TEXT NOT NULL"));

		return new String[] { sqlSMSPatent, sqlSchedulerSMS, sqlWeekOneSMS,
				sqlSaveSendSMS };
	}

	@Override
	protected String[] getTableNames() {
		return new String[] { TABLE_SMSPATENT, TABLE_SCHEDULERSMS,
				TABLE_WEEKONESMS, TABLE_SAVESENDSMS };
	}

	/* ----------------------------------------------------------------------- */
	/* Instance methods */
	/* ----------------------------------------------------------------------- */
	@Override
	public synchronized void execSQL(String sql) {
		// TODO Auto-generated method stub
		super.execSQL(sql);
	}

	/**********************************************************************************************
	 * 
	 * sms patent
	 * 
	 *********************************************************************************************/
	public int addSMSPatent(Sms s) {
		return insert(TABLE_SMSPATENT,
				new String[] { FIELD_SMSPATENT_MESSAGE },
				new Object[] { s.message });
	}

	public List<Sms> getListSMSPatent() {
		Cursor c = null;
		List<Sms> entries = new ArrayList<Sms>();
		try {
			c = db.query(TABLE_SMSPATENT, new String[] { FIELD_SMSPATENT_KEY,
					FIELD_SMSPATENT_MESSAGE }, null, null, null, null, null);
			if (c.moveToFirst()) {
				do {
					Sms s = new Sms(c.getInt(0), c.getString(1));
					entries.add(s);
				} while (c.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return entries;
	}

	public boolean removeAllSMSPatent() {
		try {
			return delete(TABLE_SMSPATENT, null, null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean removeSMSPatent(Sms s) {
		try {
			return delete(TABLE_SMSPATENT, FIELD_SMSPATENT_KEY + "= '" + s.ID
					+ "'", null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**********************************************************************************************
	 * 
	 * sms scheduler
	 * 
	 *********************************************************************************************/
	public int addSMSScheduler(SchedulerSMSItem s) {
		return insert(TABLE_SCHEDULERSMS, new String[] {
				FIELD_SCHEDULERSMS_NAME, FIELD_SCHEDULERSMS_PHONE,
				FIELD_SCHEDULERSMS_MESSAGE, FIELD_SCHEDULERSMS_WEEK,
				FIELD_SCHEDULERSMS_TIME, FIELD_SCHEDULERSMS_STATUS },
				new Object[] { s.name, s.phone, s.message, s.week, s.time,
						s.status });
	}

	public List<SchedulerSMSItem> getListSMSScheduler() {
		Cursor c = null;
		List<SchedulerSMSItem> entries = new ArrayList<SchedulerSMSItem>();
		try {
			c = db.query(TABLE_SCHEDULERSMS, new String[] {
					FIELD_SCHEDULERSMS_ID, FIELD_SCHEDULERSMS_NAME,
					FIELD_SCHEDULERSMS_PHONE, FIELD_SCHEDULERSMS_MESSAGE,
					FIELD_SCHEDULERSMS_WEEK, FIELD_SCHEDULERSMS_TIME,
					FIELD_SCHEDULERSMS_STATUS }, null, null, null, null, null);
			if (c.moveToFirst()) {
				do {
					SchedulerSMSItem s = new SchedulerSMSItem();
					s.id = c.getInt(0);
					s.name = c.getString(1);
					s.phone = c.getString(2);
					s.message = c.getString(3);
					s.week = c.getString(4);
					s.time = c.getString(5);
					s.status = c.getInt(6);
					entries.add(s);
				} while (c.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return entries;
	}

	public boolean removeAllSMSScheduler() {
		try {
			return delete(TABLE_SCHEDULERSMS, null, null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean removeSMSScheduler(SchedulerSMSItem s) {
		try {
			return delete(TABLE_SCHEDULERSMS, FIELD_SCHEDULERSMS_ID + "= '"
					+ s.id + "'", null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateStatusScheduler(SchedulerSMSItem s) {
		try {
			return update(TABLE_SCHEDULERSMS,
					new String[] { FIELD_SCHEDULERSMS_STATUS },
					new Object[] { s.status == 1 ? 0 : 1 },
					FIELD_SCHEDULERSMS_ID + "= '" + s.id + "'", null) > 0 ? true
					: false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateScheduler(SchedulerSMSItem s) {
		try {
			return update(TABLE_SCHEDULERSMS, new String[] {
					FIELD_SCHEDULERSMS_NAME, FIELD_SCHEDULERSMS_WEEK,
					FIELD_SCHEDULERSMS_TIME, FIELD_SCHEDULERSMS_MESSAGE,
					FIELD_SCHEDULERSMS_PHONE, FIELD_SCHEDULERSMS_STATUS },
					new Object[] { s.name, s.week, s.time, s.message, s.phone,
							s.status }, FIELD_SCHEDULERSMS_ID + "= '" + s.id
							+ "'", null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**********************************************************************************************
	 * 
	 * sms week one
	 * 
	 *********************************************************************************************/
	public int addWeekOneSMS(OneSMS os) {
		return insert(TABLE_WEEKONESMS, new String[] { FIELD_WEEKONESMS_RANDOM,
				FIELD_WEEKONESMS_FLAG }, new Object[] { os.random, os.flag });
	}

	public List<OneSMS> getListWeekOneSMS() {
		Cursor c = null;
		List<OneSMS> entries = new ArrayList<OneSMS>();
		try {
			c = db.query(TABLE_WEEKONESMS, new String[] { FIELD_WEEKONESMS_KEY,
					FIELD_WEEKONESMS_RANDOM, FIELD_WEEKONESMS_FLAG }, null,
					null, null, null, null);
			if (c.moveToFirst()) {
				do {
					OneSMS s = new OneSMS(c.getInt(0), c.getInt(1), c.getInt(2));
					entries.add(s);
				} while (c.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return entries;
	}

	public boolean updateOneSMS(OneSMS s) {
		try {
			return update(TABLE_WEEKONESMS, new String[] {
					FIELD_WEEKONESMS_RANDOM, FIELD_WEEKONESMS_FLAG },
					new Object[] { s.random, s.flag }, FIELD_WEEKONESMS_KEY
							+ "= '" + s.ID + "'", null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**********************************************************************************************
	 * 
	 * save sms
	 * 
	 *********************************************************************************************/
	public int addSendSMS(SendSms s) {
		return insert(TABLE_SAVESENDSMS, new String[] {
				FIELD_SAVESENDSMS_MESSAGE, FIELD_SAVESENDSMS_PHONE,
				FIELD_SAVESENDSMS_TIME }, new Object[] { s.message, s.phone,
				s.time });
	}

	public List<SendSms> getListSendSMS() {
		Cursor c = null;
		List<SendSms> entries = new ArrayList<SendSms>();
		try {
			c = db.query(TABLE_SAVESENDSMS, new String[] {
					FIELD_SAVESENDSMS_ID, FIELD_SAVESENDSMS_MESSAGE,
					FIELD_SAVESENDSMS_PHONE, FIELD_SAVESENDSMS_TIME }, null,
					null, null, null, null);
			if (c.moveToFirst()) {
				do {
					SendSms s = new SendSms(c.getInt(0), c.getString(1),
							c.getString(2), c.getString(3));
					entries.add(s);
				} while (c.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return entries;
	}

	public boolean removeAllSendSMS() {
		try {
			return delete(TABLE_SAVESENDSMS, null, null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean removeSendSMS(SendSms s) {
		try {
			return delete(TABLE_SAVESENDSMS, FIELD_SAVESENDSMS_ID + "= '"
					+ s.ID + "'", null) > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}
